package com.example.lab09.controller;

import com.example.lab08.entity.User;
import com.example.lab09.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: ZWY
 * @Date: 2023-06-07 - 14:41
 * @Description: com.example.lab08.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    @PostMapping("SignIn")
    private String SignIn(@RequestBody User user) {
        if (user.getUsername().equals("tom") && user.getPassword().equals("123")) {
            session.setAttribute("user", user);
            return  JwtUtil.generate(user.getUsername());
        }
        return null;
    }

    @GetMapping("users")
    private List<User> users() {
        String token = request.getHeader("Authorization"); // 从请求头中获取 Authorization 键对应的值 token
        if (JwtUtil.parse(token) == null) {
            return null;
        }
        List<User> users = new ArrayList<>();
        User user1 = new User(1, "Tom", "123");
        User user2 = new User(2, "Jerry", "456");
        users.add(user1);
        users.add(user2);
        return users;
    }


}


